package com.springboot.bigevent.controller;

import com.springboot.bigevent.pojo.Result;
import com.springboot.bigevent.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController

public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {

        //把文件内容存储到本地磁盘
        String originalFilename = file.getOriginalFilename();
        //需要保证文件名字唯一,使用uuid
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //调用AliOSSUtil上传文件
        String url = AliOssUtil.upload(filename, file.getInputStream());
        //file.transferTo(new File("C:\\Users\\陈\\Desktop\\file\\"+filename));//若名字（filename）一样，则会发生覆盖
        return Result.success("URL访问地址"+url);

    }
}
